<template>
  <div>
    <!--  show-checkbox设置可勾选框
    default-expand-all 子组件默认展开
    check-strictly在显示复选框的情况下，是否严格的遵循父子不互相关联的做法，默认为 false

       -->
    <el-tree
      ref="tree"
      node-key="id"
      check-strictly
      default-expand-all
      show-checkbox
      :data="list"
      :props="{label:'name',children:'children'}"
    />

    <el-row type="flex" height="60px" justify="end">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button type="info" @click="$emit('close')">取消</el-button>
    </el-row>

  </div>
</template>
<script>
import { getPermissionList } from '@/api/permissions'
import { transDateToTreeDate } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 更改权限管理
    async hSubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPerm({ id: this.roleId, permIds: permIds })
        console.log(res, 'assignPerm')
        // 关闭弹层
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    },

    // 获取权限列表数据
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        this.list = transDateToTreeDate(res.data)
        console.log('getPermissionList', res)
        const info = await getRoleDetail(this.roleId)
        console.log(info, 'info')
        // 通过key来设置el-tree复选框的选中状态
        // info.permIds:['6342bde5794a786704accac1', '604f7e17f900be1850edb154']
        this.$refs.tree.setCheckedKeys(info.data.permIds)
        // 保存数据
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }

}
</script>
<style>

</style>
